package com.yhhvip.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class DateTool {
	public static final String DATE_FORMAT_NORMAL = "yyyy-MM-dd";
	public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm";
	public static final String DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss:SSSZ";

	/*
	 * 得到两个日期之间的所有日期
	 */
	public static Map<String, Object> getBetweenDateMap(String startTime,
			String endTime, String initStr) {
		Map<String, Object> dateMap = new TreeMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NORMAL);
		try {
			Date begin = sdf.parse(startTime);
			Date end = sdf.parse(endTime);
			Calendar calendar = Calendar.getInstance();
			while (begin.before(end)) {
				dateMap.put(sdf.format(begin), initStr);
				calendar.setTime(begin);
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				begin = calendar.getTime();
			}
			dateMap.put(endTime, initStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateMap;
	}

	/**
	 * 日期加N天
	 */
	public static String addDay(String time, int internal)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NORMAL);
		Date date = sdf.parse(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, internal);
		date = calendar.getTime();
		String date1 = sdf.format(date);
		return date1;
	}

	public static final String getDate(Date aDate) {
		SimpleDateFormat df = null;
		String returnValue = "";
		if (aDate != null) {
			df = new SimpleDateFormat(DATE_FORMAT);
			returnValue = df.format(aDate);
		}
		return (returnValue);
	}

	/**
	 * 计算给开始日期 结束日期 的间隔天数 yyyy-MM-dd
	 */
	public static int getDayLength(String start_date, String end_date)
			throws Exception {
		Date fromDate = getStrToDate(start_date, DATE_FORMAT_NORMAL); // 开始日期
		Date toDate = getStrToDate(end_date, DATE_FORMAT_NORMAL); // 结束日期
		long from = fromDate.getTime();
		long to = toDate.getTime();
		// 一天等于多少毫秒：24*3600*1000
		int day = (int) ((to - from) / (24 * 60 * 60 * 1000));
		return day;
	}

	/**
	 * 字符串转为日期
	 */
	public static Date getStrToDate(String date, String fomtter)
			throws Exception {
		DateFormat df = new SimpleDateFormat(fomtter);
		return df.parse(date);
	}

	/**
	 * 将treemap的键名和键值存放到二维数组中
	 */
	public static String[][] mapToArray(Map<String, Object> dateMap) {
		String[][] dateArray = new String[2][dateMap.size()];
		Iterator key = dateMap.keySet().iterator();
		int i = 0;
		while (key.hasNext()) {
			String keyt = (String) key.next();
			dateArray[0][i] = keyt;
			dateArray[1][i++] = dateMap.get(keyt).toString();
		}
		return dateArray;
	}

	/**
	 * 日期加N天
	 */
	public static Date addDay1(String time, int internal) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NORMAL);
		SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT_UTC);
		Date date = sdf.parse(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, internal);
		// calendar.add(Calendar.HOUR_OF_DAY, 8);
		date = calendar.getTime();
		String date1 = sdf1.format(date);
		Date date2 = sdf1.parse(date1);
		return date2;
	}

	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0)
			return "00:00";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = unitFormat(minute) + "分" + unitFormat(second) + "秒";
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + "时" + unitFormat(minute) + "分"
						+ unitFormat(second) + "秒";
			}
		}
		return timeStr;
	}

	public static String secToTime1(long time) {
		String timeStr = null;
		long hour = 0;
		long minute = 0;
		long second = 0;
		if (time <= 0)
			return "0" + "秒";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = unitFormat1(minute) + "分" + unitFormat1(second) + "秒";
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat1(hour) + "时" + unitFormat1(minute) + "分"
						+ unitFormat1(second) + "秒";
			}
		}
		return timeStr;
	}

	public static String unitFormat1(long i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = Long.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}

	public static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = Integer.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}

	public static String formatDate(Date date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(date);
	}

	// 计算距离当前时间的最近的日期（最近日期=当前日期-天数）
	public static String recentlyDate(int days) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendarDate = Calendar.getInstance();
		calendarDate.add(Calendar.DATE, -days);
		Date currentDate = calendarDate.getTime();
		String recentlyDate = sdf.format(currentDate);
		return recentlyDate;
	}

	// 计算该日期加一天后的日期
	public static String addOneDay(String time) {
		String dateTime = null;
		try {
			Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(time);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DATE, 1);
			dateTime = (new SimpleDateFormat("yyyy-MM-dd")).format(cal
					.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTime;
	}

}
